<template>
  <div class="app">
    <mogo-login @goBegin="loginAfter"
      v-show="showOne === 0"
      :deviceId="deviceId"></mogo-login>
    <mogo-begin
      v-show="showOne === 1"
      ref="begin"
      :deviceId="deviceId"
      :deviceBikeTotal="deviceBikeTotal"
      :userEmail="userEmail"
      :userAddress="userAddress"
      :giveThirdEmail="giveThirdEmail"
      :giveThirdAddress="giveThirdAddress"
      :deviceNum="deviceNum"
      :totleTime="totleTime || 0"
      :checkTime="checkTime"></mogo-begin>
      <!-- @openChangePage="openChangeAddress" -->
  </div>
</template>
<script>
import { request } from './utils/request'
import { openWebview } from '@/utils/webview'
import { getDeviceId, formatSeconds } from '@/utils/envent'
import _login from './components/login.vue'
import _begin from './components/begin.vue'
import { checkUpdate } from './utils/update'

export default {
  components: {
    'mogo-login': _login,
    'mogo-begin': _begin
  },
  data() {
    return {
      deviceId: '',
      showOne: 0,
      deviceBikeTotal: '',
      userEmail: '',
      userAddress: '',
      giveThirdEmail: '',
      giveThirdAddress: '',
      deviceNum: '',
      totleTime: '',
      checkTime: ''
    }
  },
  created() {
    checkUpdate() // 检测更新
    var mainActivity = plus.android.runtimeMainActivity()
    var Settings = plus.android.importClass('android.provider.Settings')

    // 更改系统状态栏的字体颜色
    plus.navigator.setStatusBarStyle('light')
    this.deviceId = getDeviceId()
    this.checkBinding()
    document.addEventListener(
      'netchange',
      function() {
        let network = plus.networkinfo.getCurrentType()
        switch (network) {
          case plus.networkinfo.CONNECTION_NONE:
            plus.nativeUI.toast('您的网络已断开')
            break
          case plus.networkinfo.CONNECTION_WIFI:
            plus.nativeUI.toast('当前是wiWIFI环境')
            break
        }
      },
      false
    ) //监听网络变化
  },
  methods: {
    loginAfter(val) {
      // console.log(val, '--loginAfter--')
      this.deviceNum = val.deviceName
      this.showOne = 1
      this.$refs.begin.firstGetData()
    },
    checkBinding() {
      console.log('deviceId', this.deviceId)
      request({
        method: 'post',
        url: DOUBANAPI + '/checkBinding',
        data: {
          device_id: this.deviceId
        }
      })
        .then(res => {
          console.log(res)
        })
        .catch(err => {
          console.log('checkBinding-err', err)
          this.showOne = 1
          this.deviceBikeTotal = err.response.data.extra.Log
            ? err.response.data.extra.Log.bites
            : 0 // 挖币数
          this.userEmail = err.response.data.extra.User.email // 用户邮箱
          this.userAddress = err.response.data.extra.User.wallet_address // 用户邮箱地址
          this.giveThirdEmail = err.response.data.extra.trusteeship_email //托管钱包邮箱
          this.giveThirdAddress =
            err.response.data.extra.trusteeship_wallet_address // 托管钱包地址
          this.deviceNum = err.response.data.extra.device_name // 设备编号

          this.totleTime = err.response.data.extra.Log
            ? formatSeconds(err.response.data.extra.Log.accumulative_time)
            : 0 // 累计挖矿时间
          this.checkTime = err.response.data.extra.dig_total_time // 检测时间
          if (err.response.data.extra.device_status == '空闲中') {
            this.$refs.begin.firstGetData() // 自动开始挖矿
          }
          localStorage.removeItem('info')
        })
    }
    // openChangeAddress() {
    // openWebview(
    //   {
    //     url: './changeAddress.index.html',
    //     id: 'changeAddress.index'
    //   },
    //   {
    //     bounce: 'none'
    //   },
    //   {
    //     address: this.address
    //   }
    // )
    // }
  }
}
</script>

<style lang="less">
.app {
  height: 100%;
  overflow: hidden;
}
</style>
